var chipTracContext;
var req;
function toggleInternalComments(commentsGroupId)
{
    var comments = document.getElementById("internalCommentsFoot")
    var commentsHead = document.getElementById("internalCommentsHead");
    if ( commentsHead.hasChildNodes() )
    {
        commentsHead.parentNode.removeChild(commentsHead);

        var commentsTable = document.getElementById( "internalCommentsTable" );
        var thead = commentsTable.createTHead();
        thead.id = "internalCommentsHead"
        /*var commentsFoot = document.getElementById("internalCommentsFoot");
        commentsFoot.parentNode.removeChild(commentsFoot);
        var tfoot = commentsTable.createTFoot();
        tfoot.id = "internalCommentsFoot"*/
    }
    else
    {
        var row = commentsHead.insertRow();
        //row.bgColor = "#FFFFEE";
        var cell = row.insertCell();
        cell.colSpan = "3";
        cell.width = "90%";
        cell.vAlign = "bottom";
        cell.innerHTML = "<textarea rows=6 cols=70 name='internalComment' class='txfield' maxlength='400'></textarea>&nbsp;<input type='button' value='Add Comment' onclick='updateInternalComment(\"" +commentsGroupId+ "\")'>";
        /*cell = row.insertCell();
        cell.width = "10%";
        cell.vAlign = "bottom";
        cell.innerHTML = "<input type='button' value='Add Comment' onclick='updateInternalComment(\"" +commentsGroupId+ "\")'>";*/

        row = commentsHead.insertRow();
        cell = row.insertCell();
        cell.colSpan = "3";
        cell.bgColor = "#993333"
        cell.className = "NArial";
        cell.id = "requestStatusMsg"
        cell.innerHTML = "Processing..."

        showComments(commentsGroupId);
    }
}
function showComments(commentsGroupId)
{
    // Obtain an XMLHttpRequest instance
    req = newXMLHttpRequest();
    // Set the handler function to receive callback notifications
    // from the request object
    var handlerFunction;
    handlerFunction = getReadyStateHandler(req, commentsXMLHandler);
    req.onreadystatechange = handlerFunction;
    // Open an HTTP POST connection to the shopping cart servlet.
    // Third parameter specifies request is asynchronous.
    req.open("POST", chipTracContext + "/internalComments.do", true);
    // Specify that the body of the request contains form data
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // Send form encoded data
    req.send("action=getAllComments&commentsGroupId=" + commentsGroupId);
}
function commentsXMLHandler(xmlComments)
{
    if ( req )
    {
        delete req;
    }
    var requestStatusMsg = document.getElementById( "requestStatusMsg" );
    requestStatusMsg.innerHTML = "Processing..."
    var commentsBody = document.getElementById("internalCommentsFoot");
    var commentsTable = document.getElementById( "internalCommentsTable" );
    commentsBody.parentNode.removeChild(commentsBody);
    var tfoot = commentsTable.createTFoot();
    tfoot.id = "internalCommentsFoot"

    commentsBody = document.getElementById("internalCommentsFoot");

    //delete all the comments before showing
    // Get the root "cart" element from the document
    var comments = xmlComments.getElementsByTagName("commts")[0].getElementsByTagName("commt");
    for (var k = 0; k < comments.length; k++)
    {
        var comment =  comments[k];
        var user = comment.getAttribute( "user" );
        var timeStamp = comment.getAttribute( "ts" );
        var commentTxt = '';
        if ( comment.firstChild != null )
        {
            commentTxt = comment.firstChild.nodeValue;
        }

        var row = commentsBody.insertRow();
        row.bgColor = ( k%2==0 )? "#DEE1E2" : "#FFFFEE";
        
        var cell = row.insertCell();
        cell.width = "10%"
        cell.vAlign = "top";
        cell.className = "NArialL";
        cell.innerHTML = user + ":";

        cell = row.insertCell();
        cell.width = "75%"
        cell.className = "bodytext";
        cell.appendChild( document.createTextNode(commentTxt) );

        cell = row.insertCell();
        cell.width="15%"
        cell.vAlign = "top";
        cell.className = "bodytext";
        cell.innerHTML = timeStamp;
    }

    requestStatusMsg.innerHTML = "Comments"
}
function updateInternalComment(commentsGroupId)
{
    var maxSize = 400;
    var internalComment = document.getElementById( "internalComment" ).value.trim();
    if ( internalComment != '' )
    {
        document.getElementById( "internalComment" ).value = "";
        if( internalComment.length > maxSize )
        {
            internalComment = internalComment.substring(0,maxSize);
        }
        internalComment = escape( internalComment );
        // Obtain an XMLHttpRequest instance
        req = newXMLHttpRequest();
        // Set the handler function to receive callback notifications
        // from the request object
        var handlerFunction;
        handlerFunction = getReadyStateHandler(req, commentsXMLHandler);
        req.onreadystatechange = handlerFunction;
        // Open an HTTP POST connection to the shopping cart servlet.
        // Third parameter specifies request is asynchronous.
        req.open("POST", chipTracContext + "/internalComments.do", true);
        // Specify that the body of the request contains form data
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        // Send form encoded data
        req.send("action=insertComment&commentsGroupId=" + commentsGroupId + "&internalComment=" + internalComment);
    }
}